package com.wanxi.product.core_sevice.impl;

import com.wanxi.product.mapper.SpuTagMapper;
import com.wanxi.product.mapper.TagMapper;
import com.wanxi.product.mapper.TypeMapper;
import com.wanxi.product.service.api.dto.SpuTagDto;
import com.wanxi.product.service.api.dto.TagDto;
import com.wanxi.product.service.api.dto.TypeDto;
import com.wanxi.product.service.api.entity.Spu;
import com.wanxi.product.service.api.entity.SpuTag;
import com.wanxi.product.service.api.entity.Tag;
import com.wanxi.product.service.api.entity.Type;
import com.wanxi.product.service.api.query.admin.TagQuery;
import com.wanxi.product.service.api.query.enterprise.SpuQuery;
import com.wanxi.product.mapper.SpuMapper;
import com.wanxi.product.core_sevice.SpuService;
import com.wanxi.product.service.api.dto.SpuDto;
import com.wanxi.product.service.api.query.enterprise.SpuTagQuery;
import com.wanxi.product.service.api.query.admin.TypeQuery;
import com.wanxi.product.util.BeanCopyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {
    @Resource
    SpuMapper spuMapper;

    @Override
    public Long addSpu(SpuDto spuDto) {
        Spu spu = BeanCopyUtils.copy(spuDto, Spu.class);
        spuMapper.add(spu);
        return spu.getSpuId();
    }

    @Override
    public List<SpuDto> findAllSpu(SpuQuery spuQuery) {
        List<Spu> spuList = spuMapper.findAll(spuQuery);
        return BeanCopyUtils.copyList(spuList,SpuDto.class);
    }

    @Override
    public SpuDto findSpuBySpuId(Long spuId) {
        Spu spu = spuMapper.findById(spuId);
        return BeanCopyUtils.copy(spu, SpuDto.class);
    }

    @Override
    public Long updateSpu(SpuDto spuDto) {
        Spu spu = BeanCopyUtils.copy(spuDto,Spu.class);
        spuMapper.update(spu);
        return spu.getSpuId();
    }

    @Override
    public int deleteSpu(Long spuId) {
        return spuMapper.delete(spuId);
    }

    @Resource
    TagMapper tagMapper;

    @Override
    public Long addTag(TagDto tagDto) {
        Tag tag = BeanCopyUtils.copy(tagDto, Tag.class);
        tagMapper.add(tag);
        return tag.getTagId();
    }

    @Override
    public TagDto findTagByTagId(Long tagId) {
        return tagMapper.findById(tagId);
    }

    @Override
    public List<TagDto> findAllTag(TagQuery tagQuery) {
        List<Tag> tagList = tagMapper.findAll(tagQuery);
        return BeanCopyUtils.copyList(tagList,TagDto.class);
    }

    @Override
    public int deleteTag(Long tagId) {
        return tagMapper.delete(tagId);
    }

    @Resource
    SpuTagMapper spuTagMapper;

    @Override
    public Long addSpuTag(SpuTagDto spuTagDto) {
        SpuTag spuTag = BeanCopyUtils.copy(spuTagDto, SpuTag.class);
        spuTagMapper.add(spuTag);
        return spuTag.getId();
    }

    @Override
    public List<SpuTag> findAllSpuTag(SpuTagQuery spuTagQuery) {
        return spuTagMapper.findAll(spuTagQuery);
    }

    @Override
    public int deleteBySpuId(Long spuId) {
        return spuTagMapper.delete(spuId);
    }

    @Resource
    TypeMapper typeMapper;

    @Override
    public Long addType(TypeDto typeDto) {
        Type type = BeanCopyUtils.copy(typeDto,Type.class);
        typeMapper.add(type);
        return type.getTypeId();
    }

    @Override
    public TypeDto findTypeByTypeId(Long typeId) {
        Type type = typeMapper.findById(typeId);
        return BeanCopyUtils.copy(type,TypeDto.class);
    }

    @Override
    public List<TypeDto> findAllType(TypeQuery typeQuery) {
        List<Type> typeList = typeMapper.findAll(typeQuery);
        return BeanCopyUtils.copyList(typeList,TypeDto.class);
    }

    @Override
    public Long updateType(TypeDto typeDto) {
        Type type = BeanCopyUtils.copy(typeDto,Type.class);
        typeMapper.update(type);
        return type.getTypeId();
    }

    @Override
    public int deleteType(Long typeId) {
        return typeMapper.delete(typeId);
    }
}
